<template>
  <div>
    <h2>入住登记</h2>
    <el-button type="primary" @click="openAdd()">新增入住</el-button>
    <el-table :data="tableData" row-key="checkinId">
      <el-table-column prop="elderName" label="老人姓名" />
      <el-table-column prop="bedId" label="床位ID" />
      <el-table-column prop="checkinTime" label="入住时间" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="danger" @click="del(row.checkinId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="show" title="新增入住">
      <el-form :model="form" label-width="100px">
        <el-form-item label="老人姓名"><el-input v-model="form.elderName" /></el-form-item>
        <el-form-item label="年龄"><el-input v-model="form.elderAge" /></el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.elderGender">
            <el-option label="男" value="M" />
            <el-option label="女" value="F" />
          </el-select>
        </el-form-item>
        <el-form-item label="电话"><el-input v-model="form.elderPhone" /></el-form-item>
        <el-form-item label="地址"><el-input v-model="form.elderAddress" /></el-form-item>
        <el-form-item label="健康状况"><el-input v-model="form.healthStatus" /></el-form-item>
        <el-form-item label="家属姓名"><el-input v-model="form.familyName" /></el-form-item>
        <el-form-item label="家属电话"><el-input v-model="form.familyPhone" /></el-form-item>
        <el-form-item label="关系"><el-input v-model="form.relationship" /></el-form-item>
        <el-form-item label="床位ID"><el-input v-model="form.bedId" /></el-form-item>
        <el-form-item label="收费方案"><el-input v-model="form.feeScheme" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getCheckinList, addCheckin, deleteCheckin } from '@/api/checkin'

const tableData = ref([])
const show = ref(false)
const form = ref({})

const load = async () => {
  const res = await getCheckinList()
  tableData.value = res.data
}

const openAdd = () => {
  form.value = {}
  show.value = true
}

const submit = async () => {
  await addCheckin(form.value)
  show.value = false
  load()
}

const del = async (id) => {
  await deleteCheckin(id)
  load()
}

onMounted(load)
</script>
